<script setup lang="ts">
import applicationApi from '@/api/application'
import OpeningRemarks from '@/views/agent/chat/components/OpeningRemarks.vue'
import StepLoading from '@/views/agent/chat/components/StepLoading.vue'
import UploadFile from '@/views/agent/chat/components/UploadFile.vue'
import { computed, ref } from 'vue'
import {
  BidDocumentReviewProcessLoading,
  speechPreparationFormatAgent,
  speechPreparationResultAgent
} from '@/views/agent/chat/components/data'

const tip = 'AI标书审查;仅审查文字部分，不包含图片等资质部分;'


const props = withDefaults(
  defineProps<{
    appId?: any
    chatId?: string
    applicationDetails: any
  }>(),
  {
  }
)
const step = ref(0)
const loading = ref(false)
const topTips = ref(tip)

const file: any = ref({
  zhao: [],
  tou: []
})
const chatInfo: any = ref({
  zhaoChatId: '',
  zhaoContent: [],
  touChatId: '',
  touContent: [],
})

const resultContent = ref("")


function onBack () {
  step.value = 0;
}
const setDocumentList = async (val: any, chatId: string, key: string) => {
  file.value[key] = val
  if (key === 'zhao') {
    // chatInfo.value.zhaoChatId = chatId || ''
    if (!chatInfo.value.zhaoChatId) {
      const chatData = await applicationApi.getChatOtherOpen(speechPreparationFormatAgent.appId, speechPreparationFormatAgent.apiKey)
      chatInfo.value.zhaoChatId = chatData.data
      return
    }
  }
  if (key === 'tou') {
    // chatInfo.value.touChatId = chatId || ''
    if (!chatInfo.value.touChatId) {
      const chatData = await applicationApi.getChatOtherOpen(speechPreparationResultAgent.appId, speechPreparationResultAgent.apiKey)
      chatInfo.value.touChatId = chatData.data
      return
    }
  }
}

const hasSubmit = computed(() => {
  return file.value.tou.length > 0 && file.value.zhao.length > 0 && !loading.value
})

async function onSubmit () {
  step.value = 1;
  try {
    loading.value = true;
    const zhaoData = await applicationApi.postChatMessageNew(chatInfo.value.zhaoChatId, { document_list: file.value.zhao, stream: false, message: '请帮我总结招标文件核心', apiKey:  speechPreparationFormatAgent.apiKey })
    const touData = await applicationApi.postChatMessageNew(chatInfo.value.touChatId, { document_list: file.value.tou, stream: false, message: zhaoData.data?.content, apiKey:  speechPreparationResultAgent.apiKey })
    resultContent.value =  touData.data?.content || '--'
  } catch (e) {
    console.log(e)
  } finally {
    loading.value = false;
  }
}

</script>

<template>
  <OpeningRemarks class="mb-12" :title="props.applicationDetails.name" :description="props.applicationDetails.desc"  :avatar="props.applicationDetails.avatar"/>
  <el-card class="mt-12 mb-12" shadow="never" v-if="step !== 1">
    <div class="ai-start">
      <div class="ai-start-tips">
        <el-icon @click="onBack" v-if="step === 1"><ArrowLeftBold /></el-icon>
        <div class="ai-start-tips-ellipsis">
          {{ topTips }}
        </div>
      </div>
      <el-button type="primary" size="small" @click="onSubmit" :disabled="!hasSubmit">开始审查</el-button>
    </div>
  </el-card>
  <el-row :gutter="10" v-if="step === 0">
    <el-col :span="12">
      <UploadFile :app-id="speechPreparationFormatAgent.appId" :api-key="speechPreparationFormatAgent.apiKey" :show-file-list="true" @update="(val, id) => setDocumentList(val, id, 'zhao')" :max-files="2" name="招标文件" tip="支持格式：doc、docx格式文件<br/>至多支持2个文件"  accept=".docx,.doc" />
    </el-col>
    <el-col :span="12">
      <UploadFile :app-id="speechPreparationResultAgent.appId" :api-key="speechPreparationResultAgent.apiKey" :show-file-list="true" @update="(val, id) => setDocumentList(val, id, 'tou')" :max-files="2" name="投标文件" tip="支持格式：doc、docx格式文件<br/>至多支持2个文件"  accept=".docx,.doc" />
    </el-col>
  </el-row>
  <StepLoading v-if="step === 1" :data="BidDocumentReviewProcessLoading" :loading="loading" :result-content="resultContent" :applicationDetails="props.applicationDetails">
    <!--el-card>
      <div class="card-title mb-8">1、废标审查</div>
      <el-table :data="chatInfo.integratedContent" border size="small">
        <el-table-column label="审查要点" prop="name"></el-table-column>
        <el-table-column label="审查结论" prop="skills"></el-table-column>
        <el-table-column label="修改建议" prop="work"></el-table-column>
      </el-table>
    </el-card>
    <el-card class="mb-12">
      <div class="card-title mb-8">2、得分预估</div>
      <el-table :data="chatInfo.integratedContent" border size="small">
        <el-table-column label="审查要点" prop="name"></el-table-column>
        <el-table-column label="审查结论" prop="skills"></el-table-column>
        <el-table-column label="修改建议" prop="work"></el-table-column>
        <el-table-column label="得分预估" prop="education"></el-table-column>
      </el-table>
    </el-card>-->
  </StepLoading>
</template>

<style scoped lang="scss">

</style>



